package com.atguigu.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 方法名: listWithTree
     * 方法描述: 查询出所有的分类, 并拼装成父子结构
     * 修改日期: 2020/8/3 19:42
     * @param
     * @return java.util.List<com.atguigu.gulimall.product.entity.CategoryEntity>
     * @author https://javaweixin6.blog.csdn.net/
     * @throws
     */
    @Override
    public List<CategoryEntity> listWithTree() {
        //查出分类表所有数据
        List<CategoryEntity> entityList = baseMapper.selectList(null);

        //查询出所有的一级分类数据.  使用stream的api
        List<CategoryEntity> firstEntityList = entityList.stream().
                filter(categoryEntity -> categoryEntity.getParentCid() == 0).map((menu) -> {
                    //传递当前的商品分类, 和所有的分类,递归查询出子分类
                    menu.setChildren( getChildrens(menu,entityList));
                    return menu;
        } ).sorted((menu1,menu2)->{
            //进行排序
            return (menu1.getSort() ==null?0:menu1.getSort())- (menu2.getSort()==null?0 : menu2.getSort());
        }).collect(Collectors.toList());
        return firstEntityList;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO 1.检查当前删除的菜单,  是否被别的地方引用.

        //逻辑删除
        baseMapper.deleteBatchIds(asList);
    }

    /**
     * 递归查询子父类
     * @param root   当前节点的父id
     * @param all    所有的商品分类
     * @return
     */
    private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> childrenList = all.stream().filter(categoryEntity -> {
            //如果
            boolean b = categoryEntity.getParentCid() == root.getCatId();
            return b;
        }).map(categoryEntity -> {
            //子菜单可能还有子菜单, 因此递归查询 ,   查询出子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1, menu2) -> {
            //进行排序
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return childrenList;
    }


}